local yanjiao = fk.CreateSkill {

  name = "joy__yanjiao",

  tags = {  },

}



yanjiao:addEffect("active", {
  name = "joy__yanjiao",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#joy__yanjiao",
  can_use = function(self, player)
    return player:usedSkillTimes(yanjiao.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local n = 3 + player:getMark("@joy__yanjiao")
    room:setPlayerMark(player, "@joy__yanjiao", 0)
    local cards = room:getNCards(n)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = yanjiao.name,
    })
    local m = 0
    local i = 1
    for _, id in ipairs(cards) do
      local card = Fk:getCardById(id)
      m = m + Fk:getCardById(id).number
      room:addCardMark(card,"joy_yj-phase",m)
    end
    local get = room:askForPoxi(target, "joy__yanjiao_count", {{ yanjiao.name, cards}}, m, true)
    if #get > 0 then
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(get)
      room:obtainCard(target, dummy, true, fk.ReasonPrey)
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if target ~= player and not player.dead and #cards > 0 then
      local dummy2 = Fk:cloneCard("dilu")
      dummy2:addSubcards(cards)
      room:obtainCard(player, dummy2, true, fk.ReasonPrey)
    elseif target == player and #cards > 0 then
        room:moveCards({
          ids = cards,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
        })
    end
  end,
})

return yanjiao